﻿//==========================================================
// 此代码由TilverNET代码生成工具自动生成，请不要修改
// 代码生成器核心库版本号：1.2
// 作者：landy
// 联系方式：(QQ)86566178
// 创建时间：2013/10/25 14:00:35
//==========================================================

using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using MySql.Data;
using Tilver.Data;
using Tilver.Data.DataPager;

namespace Tilver.Mysql.DAL.Tilverdb
{
    #region EO

    /// <summary>
    ///  《viewcustomer(实体类)》
    /// </summary>
    [Serializable]
    public class ViewcustomerEO : IRowMapper<ViewcustomerEO>
    {
        protected int _cid;
        protected int? _cityId;
        protected string _userName;
        protected string _phone;
        protected string _address;
        protected DateTime? _recDate;
        protected sbyte _cType;
        protected string _code;
        protected string _name;


        /// <summary>
        ///  《[字段]  [int]》
        /// </summary>
        public int Cid
        {
            get { return _cid; }
            set { _cid = value; }
        }

        /// <summary>
        ///  《[字段]  [int]》
        /// </summary>
        public int? CityId
        {
            get { return _cityId; }
            set { _cityId = value; }
        }

        /// <summary>
        ///  《[字段]  [varchar]》
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set
            {
                if (value != null && value.Length > 150)
                    throw new ArgumentOutOfRangeException("Invalid value for UserName", value, value == null ? "null" : value.ToString());
                _userName = value;
            }
        }

        /// <summary>
        ///  《[字段]  [varchar]》
        /// </summary>
        public string Phone
        {
            get { return _phone; }
            set
            {
                if (value != null && value.Length > 60)
                    throw new ArgumentOutOfRangeException("Invalid value for Phone", value, value == null ? "null" : value.ToString());
                _phone = value;
            }
        }

        /// <summary>
        ///  《[字段]  [varchar]》
        /// </summary>
        public string Address
        {
            get { return _address; }
            set
            {
                if (value != null && value.Length > 300)
                    throw new ArgumentOutOfRangeException("Invalid value for Address", value, value == null ? "null" : value.ToString());
                _address = value;
            }
        }

        /// <summary>
        ///  《[字段]  [datetime]》
        /// </summary>
        public DateTime? RecDate
        {
            get { return _recDate; }
            set { _recDate = value; }
        }

        /// <summary>
        ///  《[字段]  [tinyint]》
        /// </summary>
        public sbyte CType
        {
            get { return _cType; }
            set { _cType = value; }
        }

        /// <summary>
        ///  《[字段]  [varchar]》
        /// </summary>
        public string Code
        {
            get { return _code; }
            set
            {
                if (value != null && value.Length > 30)
                    throw new ArgumentOutOfRangeException("Invalid value for Code", value, value == null ? "null" : value.ToString());
                _code = value;
            }
        }

        /// <summary>
        ///  《[字段]  [varchar]》
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                if (value != null && value.Length > 150)
                    throw new ArgumentOutOfRangeException("Invalid value for Name", value, value == null ? "null" : value.ToString());
                _name = value;
            }
        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ViewcustomerEO()
        {
        }

        /// <summary>
        /// 自定义构造函数
        /// </summary>
        /// <param name = "cid"></param>
        /// <param name = "cityId"></param>
        /// <param name = "userName"></param>
        /// <param name = "phone"></param>
        /// <param name = "address"></param>
        /// <param name = "recDate"></param>
        /// <param name = "cType"></param>
        /// <param name = "code"></param>
        /// <param name = "name"></param>
        public ViewcustomerEO(int cid, int? cityId, string userName, string phone, string address, DateTime? recDate, sbyte cType, string code, string name)
        {
            _cid = cid;
            _cityId = cityId;
            _userName = userName;
            _phone = phone;
            _address = address;
            _recDate = recDate;
            _cType = cType;
            _code = code;
            _name = name;
        }


        /// <summary>
        /// 将结果集映射成实体对象
        /// </summary>
        /// <param name = "reader">只进结果集流</param>
        /// <return></return>
        public ViewcustomerEO MapRow(IDataReader reader)
        {
            var ret = new ViewcustomerEO();
            ret.Cid = (int)reader["Cid"];
            ret.CityId = (reader["CityId"] is DBNull) ? null : (int?)reader["CityId"];
            ret.UserName = (reader["UserName"] is DBNull) ? null : (string)reader["UserName"];
            ret.Phone = (reader["Phone"] is DBNull) ? null : (string)reader["Phone"];
            ret.Address = (reader["Address"] is DBNull) ? null : (string)reader["Address"];
            ret.RecDate = (reader["RecDate"] is DBNull) ? null : (DateTime?)reader["RecDate"];
            ret.CType = (sbyte)reader["CType"];
            ret.Code = (reader["Code"] is DBNull) ? null : (string)reader["Code"];
            ret.Name = (reader["Name"] is DBNull) ? null : (string)reader["Name"];
            return ret;
        }

        /// <summary>
        /// 将结果集映射成实体对象，返回映射方法
        /// </summary>
        /// <return></return>
        public Func<IDataReader, ViewcustomerEO> GetMapper()
        {
            return MapRow;
        }


    }


    #endregion
    #region MO

    /// <summary>
    ///  《viewcustomer(逻辑类)》
    /// </summary>
    public class ViewcustomerMO : DbManagerBase
    {

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name = "routeData">路由数据</param>
        public ViewcustomerMO(object routeData = null) : base(routeData) { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name = "connectionStringName">连接字符串名称</param>
        public ViewcustomerMO(string connectionStringName) : base(connectionStringName) { }

        #endregion

        #region 私有方法
        /// <summary>
        /// 数据库访问操作方法
        /// </summary>
        /// <param name = "sql">SQL语句</param>
        /// <return>返回数据操作对象</return>
        private DbAdoBase AdoBase(string sql)
        {
            return DataBase.CreateDbAdo(sql, CommandType.Text);
        }

        #endregion

        #region Select方法集合
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "cid"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<ViewcustomerEO> SelectByCid(int cid, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer WHERE Cid=@Cid");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), string.Empty);
            sql.Append((top > 0) ? string.Format(" LIMIT {0} ", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@Cid", cid, DbType.Int32, 0)
                            .ExecuteList(new ViewcustomerEO().GetMapper(), tm);
        }
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "cityId"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<ViewcustomerEO> SelectByCityId(int? cityId, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer WHERE CityId=@CityId");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), string.Empty);
            sql.Append((top > 0) ? string.Format(" LIMIT {0} ", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@CityId", cityId, DbType.Int32, 0)
                            .ExecuteList(new ViewcustomerEO().GetMapper(), tm);
        }
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "userName"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<ViewcustomerEO> SelectByUserName(string userName, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer WHERE UserName=@UserName");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), string.Empty);
            sql.Append((top > 0) ? string.Format(" LIMIT {0} ", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@UserName", userName, DbType.String, 150)
                            .ExecuteList(new ViewcustomerEO().GetMapper(), tm);
        }
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "phone"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<ViewcustomerEO> SelectByPhone(string phone, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer WHERE Phone=@Phone");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), string.Empty);
            sql.Append((top > 0) ? string.Format(" LIMIT {0} ", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@Phone", phone, DbType.String, 60)
                            .ExecuteList(new ViewcustomerEO().GetMapper(), tm);
        }
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "address"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<ViewcustomerEO> SelectByAddress(string address, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer WHERE Address=@Address");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), string.Empty);
            sql.Append((top > 0) ? string.Format(" LIMIT {0} ", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@Address", address, DbType.String, 300)
                            .ExecuteList(new ViewcustomerEO().GetMapper(), tm);
        }
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "recDate"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<ViewcustomerEO> SelectByRecDate(DateTime? recDate, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer WHERE RecDate=@RecDate");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), string.Empty);
            sql.Append((top > 0) ? string.Format(" LIMIT {0} ", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@RecDate", recDate, DbType.DateTime, 0)
                            .ExecuteList(new ViewcustomerEO().GetMapper(), tm);
        }
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "cType"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<ViewcustomerEO> SelectByCType(sbyte cType, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer WHERE CType=@CType");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), string.Empty);
            sql.Append((top > 0) ? string.Format(" LIMIT {0} ", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@CType", cType, DbType.SByte, 0)
                            .ExecuteList(new ViewcustomerEO().GetMapper(), tm);
        }
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "code"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<ViewcustomerEO> SelectByCode(string code, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer WHERE Code=@Code");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), string.Empty);
            sql.Append((top > 0) ? string.Format(" LIMIT {0} ", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@Code", code, DbType.String, 30)
                            .ExecuteList(new ViewcustomerEO().GetMapper(), tm);
        }
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "name"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<ViewcustomerEO> SelectByName(string name, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer WHERE Name=@Name");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), string.Empty);
            sql.Append((top > 0) ? string.Format(" LIMIT {0} ", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@Name", name, DbType.String, 150)
                            .ExecuteList(new ViewcustomerEO().GetMapper(), tm);
        }

        /// <summary>
        /// 按自定义条件查询
        /// </summary>
        /// <param name = "where">自定义条件,where子句</param>
        /// <param name = "top">获取行数</param>
        /// <param name = "sort">排序表达式</param>
        /// <param name = "paras">where子句中定义的参数集合</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<ViewcustomerEO> Select(string where, int top, string sort, IEnumerable<DbParameter> paras, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer ");
            if (!string.IsNullOrEmpty(where)) format.AppendFormat(" WHERE {0} ", where);
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), string.Empty);
            sql.Append((top > 0) ? string.Format(" LIMIT {0} ", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddParameters(paras)
                            .ExecuteList(new ViewcustomerEO().GetMapper(), tm);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name = "where">条件表达式</param>
        /// <param name = "sort">排序表达式</param>
        /// <param name = "paras">传入参数</param>
        /// <param name = "pageSize">页大小</param>
        /// <param name = "pageIndex">页码</param>
        /// <param name = "recordCount">总记录数，由于计算总记录数影响数据库性能，只有小于或等于0的情况下才返回总记录数</param>
        /// <return>分页数据</return>
        public List<ViewcustomerEO> SelectPager(string where, string sort, IList<DbParameter> paras, long pageSize, long pageIndex, ref long recordCount)
        {
            var sql = new StringBuilder();
            sql.AppendFormat(@"SELECT Cid, CityId, UserName, Phone, Address, RecDate, CType, Code, Name FROM viewcustomer");
            if (!string.IsNullOrEmpty(where)) sql.AppendFormat(" WHERE {0} ", where);
            if (!string.IsNullOrEmpty(sort)) sql.AppendFormat(" ORDER BY {0} ", sort);
            var pager = this.DataBase.CreatePager(sql.ToString(), pageSize);
            pager.AddParameters(paras);
            if (recordCount <= 0)
            {
                recordCount = pager.GetRecordCount();
            }
            return pager.GetPageData(pageIndex).MapToList(new ViewcustomerEO().GetMapper());
        }

        #endregion

        #region Insert方法集合
        #endregion

        #region Update方法集合
        #endregion

        #region Delete方法集合
        #endregion

        #region  自定义方法集合
        #endregion

    }


    #endregion
}
